<?php

namespace Home\Controller;

use Think\Controller;

class RegisterController extends Controller
{

    public function index()
    {

        $this->display();
    }

    public function add()
    {
        $_POST['pic'] = $this->upload('pic');
        // dump($info);
        $user = D('register');
        //判断验证码是否匹配
        $data = $user->create();
        // var_dump($user);
        if ($data) {
            dump($data);
            //$u 添加插入后最新一条数据id
            if ($u = $user->add($data)) {
                // $this->sendMail($u,$data['email']);
                $this->success('注册成功', U('Login/index'));
            } else {
                $this->error('注册失败');
            }
        } else {
            $this->error($user->getError());
        }
    }

    protected function upload($inputName)
    {
        $upload           = new \Think\Upload(); // 实例化上传类
        $upload->maxSize  = 3145728; // 设置附件上传大小
        $upload->exts     = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录
        // 上传单个文件
        $info = $upload->uploadOne($_FILES[$inputName]);
        if (!$info) {
            // 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
// 上传成功 获取上传文件信息
            return $info['savepath'] . $info['savename'];

        }
    }

    public function sendMail($uid, $email)
    {

        import('Org.Net.Mail');
        $url  = 'http://' . I('server.SERVER_NAME') . U('Home/User/active', ['id' => $uid]);
        $info = "<a href='{$url}'>点击激活</a>";
        SendMail($email, '账号激活', $info, '管理员');
    }

    //方法用来激活用户
    public function active()
    {

        $uid = I('get.id');
        //更新某一个字段
        $User = D("Register"); //实例化User对象

        if ($User->where(['id' => $uid])->setField('isactive', 1)) {
            $this->success('激活成功', U('login'));
            $this->error('激活失败');
        }
    }

}
